package com.erp.app.catalog.itemCategory.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;

import com.erp.app.catalog.itemCategory.vo.ItemCategoryVo;
import com.erp.framework.exceptions.DbObjectException;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.DbObjectUtil;
import com.erp.framework.util.logger.Logger;

public class ItemCategoryCommonDao {

	public void insertRelateTabel(SimpleBaseVo model, Connection conn) throws Exception {
		PreparedStatement pStmt = null;

        ItemCategoryVo vo = (ItemCategoryVo)model;

        try{
        	//Need to use squence table later
            String sql = "insert into t_catalog_children (company_code, NODE_ID, CHILD_NODE_ID) values (?, ?, nvl((select max(node_id) + 1 from t_catalog), 1))";
            int i = 1;
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(i++, CommonUtil.nullToString(vo.getCompanyCode()));
            pStmt.setBigDecimal(i++, CommonUtil.strToBigDecimal(vo.getCategoryItemLine()));
//            pStmt.setBigDecimal(i++, CommonUtil.strToBigDecimal(vo.getCategoryItemCategory()));
            int rc = pStmt.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (pStmt != null)
                    pStmt.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }
        }
		
	}

	public void removeRelateTables(SimpleBaseVo model, Connection conn) throws DbObjectException {
		String spName = "sp_del_item_category";
        ArrayList args = new ArrayList();
        PreparedStatement pStmt = null;


        ItemCategoryVo vo = (ItemCategoryVo) model;
        try {
        	int i = 1;
        	args.add(vo.getCategoryItemLine());
        	args.add(vo.getCategoryItemCategory());
//        	pStmt.setBigDecimal(i++, CommonUtil.strToBigDecimal(vo.getCategoryItemLine()));
//            pStmt.setBigDecimal(i++, CommonUtil.strToBigDecimal(vo.getCategoryItemCategory()));
//            args.add(vo.getCompanyCode());
            
//            DbObjectUtil.callSp(spName, args, conn);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            throw new DbObjectException(e.getMessage());
        }
		
	}

//	 syn other companies' record if this master share among companies
	/*public void updateMasterRecord(SimpleBaseVo model, Connection conn) throws Exception {		
		ItemCategoryVo vo = (ItemCategoryVo) model;
		String spName = "SP_UPD_MST_ITEM_CATEGORY";
		
		ArrayList args = new ArrayList();
		args.add(vo.getCompanyCode()); // in_company_code
		args.add(ProjectConstants.DOC_TYPE_ITEM_CATEGORY); // in_type
		args.add(vo.getCategoryItemCategory()); // in_code
		DbObjectUtil.callSp(spName, args, conn);
	}*/
}
